<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 部门管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-plus" class="handle-del mr10" @click="handleAdd">添加部门 </el-button>
                <el-button type="primary" icon="el-icon-download" class="handle-del mr10" @click="handleExport">导出全部 </el-button>
                <el-input v-model="query.deptName" placeholder="部门名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                v-loading="loading"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="deptId" label="ID" align="center"></el-table-column>
                <el-table-column prop="deptName" label="部门名" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑 </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="jumper,total, prev, pager, next, sizes"
                    @size-change="handleSizeChange"
                    :current-page.sync="query.pageIndex"
                    :page-sizes="[5, 10, 20, 30]"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="open" width="30%" @close="cancel">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="部门编号" v-if="title == '编辑部门'">
                    <el-input v-model="form.deptId" placeholder="请填写部门编号"></el-input>
                </el-form-item>
                <el-form-item label="部门">
                    <el-input v-model="form.deptName" placeholder="请填写部门名称"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { listDept, delDept, addDept, updateDept, exportDept,getDept} from '@/api/dept';

export default {
    name: 'dept',
    data() {
        return {
            query: {
                deptName: '',
                pageIndex: 1,
                pageSize: 5
            },
            tableData: [],
            loading: true,
            multipleSelection: [],
            delList: [],
            departments: [
                {
                    postName: '',
                    postId: 1
                },
                {
                    postName: '',
                    postId: 2
                },
                {
                    postName: '',
                    postId: 3
                }
            ],
            department: [],

            title: '',
            open: false,
            pageTotal: 0,
            form: {},
            rules: {
                deptName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
                deptId: [{ required: true, message: '部门编号不能为空', trigger: 'blur' }]
            },
            idx: -1,
            id: -1
        };
    },
    created() {
        this.selectAllThing();
        this.getData();
    },
    methods: {
        selectAllThing() {
            this.department = [];
            this.departments.map((item) => {
                this.department.push(item.postName);
            });
        },
        getData() {
            this.loading=true;
            listDept(this.query).then((res) => {
                const data = res.data;
                this.tableData = data.data;
                this.pageTotal = data.count;
                this.loading=false;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            //重置页码
            this.query.pageIndex = 1;
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除该部门吗？', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    const $this = this;
                    delDept(row.deptId).then((res) => {
                        $this.$message.success('删除成功');
                        $this.getData();
                        $this.idx = index;
                    });
                })
                .catch(() => {});
        },
        // 表单重置
        reset() {
            this.form = {
                deptId: undefined,
                deptName: undefined
            };
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].deptId + ' ';
            }
            this.$message.error(`删除了编号为${str}`);
            this.multipleSelection = [];
        },
        // 添加操作
        handleAdd() {
            this.title = '添加部门';
            this.open = true;
        },
        // 编辑操作
        handleEdit(index, row) {
            this.title = '编辑部门';
            this.idx = index;
            const $this =this;
            getDept(row.deptId).then((res)=>{
                $this.form = res.data;
            })
            this.open = true;
        },
        // 提交按钮
        submitForm: function () {
            const $this = this;
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.deptId != undefined) {
                        updateDept(this.form).then((response) => {
                            $this.$message.success('修改成功');
                            $this.getData();
                            $this.cancel();
                        });
                    } else {
                        addDept(this.form).then((response) => {
                            $this.$message.success('添加成功');
                            $this.getData();
                            $this.cancel();
                        });
                    }
                }
            });
        },
        //取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 分页导航
        handlePageChange() {
            this.getData();
        },
        handleSizeChange(val) {
            this.query.pageSize = val;
            this.getData();
        },
        handleExport() {
            this.$confirm('是否确认导出所有岗位数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function () {
                    return exportDept();
                })
                .then((response) => {
                    this.downloadFile(response, '部门表');
                });
        }
    }
};
</script>

<style scoped>
body {
    margin: 0;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
